package control;
// <editor-fold defaultstate="collapsed" desc=" IMPORTS ">

import java.awt.event.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumMap;
import javax.swing.JButton;
import javax.swing.JTable;
import model.Cases;
import model.Event;
import model.MiljoBoven;
import model.User;
import view.MainFrame;
import view.MainPanel;
// </editor-fold>

/**
 * Listener for class MainPanel
 *
 * @author Kevin
 * @version 2015-05-25
 */
public class MainPanelListener implements ActionListener, MouseListener {

    private final MainFrame view;
    private final MiljoBoven model;
    private final MainPanel mainPanel;

    /**
     * Constructor for class MainPanelListener
     * @param mainFrame
     * @param miljoBoven
     * @param mainPanel
     */
    public MainPanelListener(MainFrame mainFrame, MiljoBoven miljoBoven, MainPanel mainPanel) {
        this.view = mainFrame;
        this.model = miljoBoven;
        this.mainPanel = mainPanel;
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        Object choice = (e.getSource());

        JButton logutButton = mainPanel.getLogoutButton();
        JButton newCaseButton = mainPanel.getNewCaseButton();

        if (choice == logutButton) {
            System.exit(0);
        } else if (choice == newCaseButton) {
            model.setCurrentCase(null);
            showCaseDetails(-1);
        }

    }

    @Override
    public void mouseClicked(MouseEvent e) {

        if (e.getClickCount() == 2) {
            JTable table = mainPanel.getTable();
            int selectedRow = table.getSelectedRow();

            String[][] temp;
            temp = mainPanel.getDataVal();
            int selectedCaseId = Integer.parseInt(temp[table.convertRowIndexToModel(selectedRow)][0]);
            showCaseDetails(selectedCaseId);
        }
    }
    // <editor-fold defaultstate="collapsed" desc=" empty @Override ">

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }
// </editor-fold>

    /**
     * Shows the detailed view for a chosen case
     * @param caseId The id of the case to show
     */
    private void showCaseDetails(int caseId) {
        boolean isNewCase;
        EnumMap<Cases.Field, Object> caseDataMap = new EnumMap<>(Cases.Field.class);
        ArrayList<EnumMap<Event.Field, Object>> events = new ArrayList<>();
        EnumMap<Cases.Field, Collection> fieldChoices = new EnumMap<>(Cases.Field.class);
        fieldChoices.put(Cases.Field.RESPONSIBLE_DEPARTMENT, Cases.Field.RESPONSIBLE_DEPARTMENT.getChoices());
        fieldChoices.put(Cases.Field.STATUS, Cases.Field.STATUS.getChoices());

        if (caseId != -1) {
            isNewCase = false;
            model.Cases thisCase = model.getCaseById(caseId);
            model.setCurrentCase(thisCase);
            caseDataMap = thisCase.toMap();

            fieldChoices.put(Cases.Field.RESPONSIBLE_ADMINISTRATOR,
                    model.getAdministratorsByDepartment(model.getCurrentUser().getDepartment()));

            for (Event event : thisCase.getEvents()) {
                EnumMap<Event.Field, Object> map = new EnumMap<>(model.Event.Field.class);
                map.put(Event.Field.EVENT_ID, event.getId());
                map.put(Event.Field.EVENT_DATE, event.getDate());
                map.put(Event.Field.EVENT_DESCRIPTION, event.getComment());
                map.put(Event.Field.EVENT_FILE, event.getFile());
                events.add(map);
            }
        } else {
            isNewCase = true;
            caseDataMap.put(Cases.Field.CASE_ID, model.getCaselist().size() + 1);
        }

        view.showDetailedPanel();
        view.getDetailedPanel().updatePanel(isNewCase, caseDataMap, events, model.getCurrentUser().getUserType(), fieldChoices, model.getCaselist().size()+1, User.getEditingRights());
    }
}
